package com.ww.springboot.boot.algorithm.plzh;

import java.util.ArrayList;
import java.util.List;

/**
 * 组合
 *
 * @author ZH500
 */
public class Combination {

    private static List<String> strings = new ArrayList<>();

    public static void main(String[] args) {
        System.out.println("排列结果 : ");
        permutationAll(5);
        System.out.println(strings);
    }

    public static List<String> permutationAll(Integer num) {
        for (int i = num; i > 0; i--) {
            permutation("", num, i);
        }
        return strings;
    }


    public static void permutation(String s, Integer length, int n) {
        if (n == 1) {
            for (int i = 1; i <= length; i++) {
                if (s.length() > 0) {
                    if (i > Integer.parseInt(s.substring(s.length() - 1))) {
                        strings.add(s + i);
                    }
                } else {
                    strings.add(s + i);
                }
            }
        } else {
            for (int i = 1; i <= length; i++) {
                if (s.length() > 0) {
                    if (i > Integer.parseInt(s.substring(s.length() - 1))) {
                        String ss = s + i;
                        permutation(ss, length, n - 1);
                    }
                } else {
                    String ss = s + i;
                    permutation(ss, length, n - 1);
                }
            }
        }
    }
}